package com.sq.imp.MyMenuManagers;

import com.sq.abstractClass.Text;

import javax.swing.*;
import java.awt.*;

public class FontStyleManager {
    private final Text textArea;
    private final JFrame frame;

    public FontStyleManager(Text textArea, JFrame frame) {
        this.textArea = textArea;
        this.frame = frame;
    }

    public void addStyleMenu(JMenuBar menu) {
        JMenu fontAndColorMenu = new JMenu("字体");
        menu.add(fontAndColorMenu);

        JMenu fontStyleMenu = new JMenu("字体样式");
        fontAndColorMenu.add(fontStyleMenu);

        JMenuItem boldItem = new JMenuItem("加粗");
        boldItem.addActionListener(e -> toggleFontStyle(Font.BOLD));
        fontStyleMenu.add(boldItem);

        JMenuItem italicItem = new JMenuItem("斜体");
        italicItem.addActionListener(e -> toggleFontStyle(Font.ITALIC));
        fontStyleMenu.add(italicItem);

        JMenu font = new JMenu("字体设置");
        JTextField fontName = new JTextField("宋体");
        fontName.addActionListener(e -> {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] availableFonts = ge.getAvailableFontFamilyNames();
            for (String availableFont : availableFonts) {
                if (availableFont.equalsIgnoreCase(fontName.getText())) { // 存在该字体，进行更改
                    textArea.setFont(new Font(fontName.getText(),
                            textArea.getFont().getStyle(), textArea.getFont().getSize()));
                    break;
                }
            }
        });
        font.add(fontName);
        fontStyleMenu.add(font);

        JMenu fontColorMenu = new JMenu("字体颜色");
        fontAndColorMenu.add(fontColorMenu);

        JMenuItem colorBlackItem = new JMenuItem("黑色");
        colorBlackItem.addActionListener(e -> textArea.setForeground(Color.BLACK));
        fontColorMenu.add(colorBlackItem);

        JMenuItem colorRedItem = new JMenuItem("红色");
        colorRedItem.addActionListener(e -> textArea.setForeground(Color.RED));
        fontColorMenu.add(colorRedItem);

        JMenuItem colorBlueItem = new JMenuItem("蓝色");
        colorBlueItem.addActionListener(e -> textArea.setForeground(Color.BLUE));
        fontColorMenu.add(colorBlueItem);

        JMenuItem colorGreenItem = new JMenuItem("绿色");
        colorGreenItem.addActionListener(e -> textArea.setForeground(Color.GREEN));
        fontColorMenu.add(colorGreenItem);

        JMenuItem customColorItem = new JMenuItem("自定义");
        customColorItem.addActionListener(e -> showCustomColorDialog());
        fontColorMenu.add(customColorItem);


    }

    private void toggleFontStyle(int fontStyle) {
        Font currentFont = textArea.getFont();
        int newStyle = currentFont.getStyle() ^ fontStyle;
        textArea.setFont(currentFont.deriveFont(newStyle));
    }

    private void showCustomColorDialog() {
        Color initialColor = textArea.getForeground();
        Color newColor = JColorChooser.showDialog(frame, "选择文本颜色", initialColor);
        if (newColor != null) {
            textArea.setForeground(newColor);
        }
    }
}